package com.hex.liyue.usermodule.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.manager.UserManager;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.commonmodule.view.FastIndexBar;
import com.hex.liyue.usermodule.adapter.RecommendListAdapter;
import com.hex.liyue.usermodule.bean.MachPhoneResultBean;
import com.hex.liyue.usermodule.presenter.IRecommendView;
import com.hex.liyue.usermodule.presenter.RecommendPresenter;

import java.util.Collections;
import java.util.Comparator;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by yukuo on 2016/4/26.
 * 这是一个邀请的界面
 */
public class RecommendActivity extends BaseActivity implements View.OnClickListener, IRecommendView {
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.tv_toobar_right)
    TextView tvToobarRight;
    @Bind(R.id.rcv_recommend)
    RecyclerView rcvRecommend;
    @Bind(R.id.fib_recommend)
    FastIndexBar fibRecommend;
    @Bind(R.id.tv_recommend_tip)
    TextView tvRecommendTip;

    private RecommendListAdapter recommendListAdapter;
    private MachPhoneResultBean machPhoneResultBean;
    private String recommendcode;//我的邀请码
    private RecommendPresenter recommendPresenter;


    @Override
    protected void bindPresenter() {
        recommendPresenter = new RecommendPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        recommendcode = UserManager.getSelfInviteCode();

        recommendPresenter.getPhoneContact();
        rcvRecommend.setLayoutManager(new LinearLayoutManager(this));
        fibRecommend.setOnLetterChangeListener(new FastIndexBar.OnLetterChangeListener() {
            @Override
            public void onLetterChange(String letter) {
                if (machPhoneResultBean != null) {
                    tvRecommendTip.setVisibility(View.VISIBLE);
                    tvRecommendTip.setText(letter);
                    for (int i = 0; i < machPhoneResultBean.data.size(); i++) {
                        if (letter.equals(machPhoneResultBean.data.get(i).pinyin)) {
                            rcvRecommend.scrollToPosition(i);
                        }
                    }
                }
            }

            @Override
            public void onUp() {
                tvRecommendTip.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_recommend;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        ivToobarBack.setVisibility(View.VISIBLE);
        tvToobarTitle.setText("发送邀请码");
        tvToobarRight.setVisibility(View.GONE);
    }

    @Override
    @OnClick({R.id.iv_toobar_back})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
        }
    }


    @Override
    public void showLod(String msg) {
        showLoad(this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }

    @Override
    public void setAdapter(final MachPhoneResultBean machPhoneResultBean) {
        //排序
        Collections.sort(machPhoneResultBean.data, new Comparator<MachPhoneResultBean.DataBean>() {
            @Override
            public int compare(MachPhoneResultBean.DataBean lhs, MachPhoneResultBean.DataBean rhs) {
                return lhs.pinyin.compareToIgnoreCase(rhs.pinyin);
            }
        });

        this.machPhoneResultBean = machPhoneResultBean;
        if (recommendListAdapter == null) {
            recommendListAdapter = new RecommendListAdapter(machPhoneResultBean);
            rcvRecommend.setAdapter(recommendListAdapter);
        }
        recommendListAdapter.setOnItemClickListener(new RecommendListAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(int postion) {
                if (!machPhoneResultBean.data.get(postion).registered.equals("1")) {
                    Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + machPhoneResultBean.data.get(postion).mobile));
                    intent.putExtra("sms_body", recommendcode+  "http://www.liyue888.com/web/");
                    startActivity(intent);
                }
            }
        });
        recommendListAdapter.notifyDataSetChanged();
    }
}
